<?php
require_once 'Kornak/Http/Crawler/Plugin/Abstract.php';
require_once 'Zend/Dom/Query.php';

class Kornak_Http_Crawler_Plugin_ExtractLinks extends Kornak_Http_Crawler_Plugin_Abstract
{
    protected function _buildLink($url, $link)
    {
        if (Zend_Uri::check($link)) {
            return $link;
        }

        $urlParts  = parse_url($url);
        $linkParts = parse_url($link);

        if (!empty($linkParts['scheme'])) {
            $protocole = $linkParts['scheme'];
        } else {
            $protocole = $urlParts['scheme'];
        }

        if ('http' != $protocole && 'https' != $protocole) {
            return false;
        }

        $uri       = Zend_Uri::factory($protocole);
        $partsName = array('host', 'port', 'username' => 'user', 'password' => 'pass', 'path', 'query');
        foreach ($partsName as $methodName => $partName) {
            if (is_string($methodName)) {
                $methodName = 'set' . ucfirst($methodName);
            } else {
                $methodName = 'set' . ucfirst($partName);
            }

            if (!empty($linkParts[$partName])) {
                $uri->$methodName($linkParts[$partName]);
            } else {
                $uri->$methodName($urlParts[$partName]);
            }
        }

        return $uri->getUri();
    }

    public function getData()
    {
        $links   = array();
        $dom     = new Zend_Dom_Query($this->_response->getBody());
        $results = $dom->query('a');
        if (0 < count($results)) {
            foreach ($results as $result) {
                $link = $this->_buildLink($this->_url, $result->getAttribute('href'));
                if (false !== $link) {
                    $links[] = $link;
                }
            }
        }
        array_unique($links);

        $this->log('Extracted ' . count($links) . ' links', Zend_Log::DEBUG);

        return $links;
    }
}